﻿$(function(){

    var ul = $('#uploadFile_ADD_TB ul.ul_lisst_views_file');
    var ulNameFileReturn = $("ul#listInputFileReturn");

    var button_Files_TB = $("#dropFile_ADD_TB")[0];
    var uploader_FIles_TB = new qq.FileUploader({
        element: button_Files_TB,
        allowedExtensions: [],
        sizeLimit: 2147483647, // max size
        action: _nameSubDomain + "/THONG_TIN_DON_VI/UploadFileThongBao",
        multiple: false,
        onSubmit: function (file, extension) {
            $('div#dropFile_ADD_TB').mask("Đang tải file lên ....");
        },
        onComplete: function (id, fileName, dataReturn) {
            
            if (dataReturn.status == "success") {

                var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"' +
                        ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span class="spanremove" data-filename="' + dataReturn._path + '"></span></li>');

                // Append the file name and file size
                tpl.find('p').text(fileName)
                             .append('<i>' + formatFileSize(dataReturn._size) + '</i>');

                // Add the HTML to the UL element
                tpl.appendTo(ul);

                // Listen for clicks on the cancel icon
                $('span.spanremove').click(function () {

                    $('div#dropFile_ADD_TB').mask("Đang xóa file ....");

                    var filename = ($(this).attr("data-filename"));
                    var _this = $(this);

                    $.ajax({ url: _nameSubDomain + "/THONG_TIN_DON_VI/DeleFile_TB?file=" + filename, async: false }).done(function (data) {

                        _this.parent('.working').fadeOut(function () {
                            $(this).remove();

                            $('div#dropFile_ADD_TB').unmask();

                        });

                        $('ul#listInputFileReturn :input[value="' + dataReturn._path + '"]').parent().remove();


                    });

                });

                // tao list Input
                var liInputReturnValue = $('<li><input name="fileUpload_TB" value="' + dataReturn._path + '"/><li>');
                liInputReturnValue.appendTo(ulNameFileReturn);

                $('div#dropFile_ADD_TB').unmask();

                return false;

            }
            else if (dataReturn.status == "successExits") {

                confirmOptionNormal("File này đã có trên hệ thống vui lòng xem lại");
                $('div#dropFile_ADD_TB').unmask();
                return false;
            }
            else {

                confirmOptionNormal("Có lỗi xảy ra trong quá trình upload ... vui lòng nhấn f5 hoặc tải lại trang web ");
                $('div#dropFile_ADD_TB').unmask();
                return false;
            }

        }
    });

    // Helper function that formats the file sizes
    function formatFileSize(bytes) {
        if (typeof bytes !== 'number') {
            return '';
        }

        if (bytes >= 1000000000) {
            return (bytes / 1000000000).toFixed(2) + ' GB';
        }

        if (bytes >= 1000000) {
            return (bytes / 1000000).toFixed(2) + ' MB';
        }

        return (bytes / 1000).toFixed(2) + ' KB';
    }

});